<template>
  <div>
    <el-form :inline="true" class="demo-form-inline" :model="cgForm">
      <el-form-item label="请选择">
        <!-- select中  v-model收集select数据 ，数据为option中的value值   -->
        <el-select
        :disabled="show"
          placeholder="一级分类"
          v-model="cgForm.category1Id"
          @change="getCategory2"
        >
          <el-option
            :label="c1.name"
            :value="c1.id"
            v-for="c1 in list1"
            :key="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="请选择">
        <el-select
          placeholder="二级分类"
          v-model="cgForm.category2Id"
          @change="getCategory3"
          :disabled="show"
        >
          <el-option
            :label="c2.name"
            :value="c2.id"
            v-for="c2 in list2"
            :key="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="请选择">
        <el-select
          placeholder="三级分类"
          v-model="cgForm.category3Id"
          @change="getCategoryId"
          :disabled="show"
        >
          <el-option
            :label="c3.name"
            :value="c3.id"
            v-for="c3 in list3"
            :key="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "CategorySelect",
  props:['show'],
  data() {
    return {
      list1: [],
      list2: [],
      list3: [],
      cgForm: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      },
    };
  },
  mounted() {
    this.getCayrgory1();
  },
  methods: {
    //获取一二三级内容
    async getCayrgory1() {
      let result = await this.$API.attr.reqGetCategory1();
      if (result.code == 200) {
        this.list1 = result.data;
      }
    },
    async getCategory2() {
      this.list3 = [];
      this.cgForm.category2Id = "";
      this.cgForm.category3Id = "";
      let { category1Id } = this.cgForm;
      this.$emit("getCategoryId", { categoryId: category1Id, level: "1" });
      let result = await this.$API.attr.reqGetCategory2(category1Id);
      if (result.code == 200) {
        this.list2 = result.data;
      }
    },
    async getCategory3() {
      this.cgForm.category3Id = "";
      let { category2Id } = this.cgForm;
      this.$emit("getCategoryId", { categoryId: category2Id, level: "2" });
      let result = await this.$API.attr.reqGetCategory3(category2Id);
      if (result.code == 200) {
        this.list3 = result.data;
      }
    },
    getCategoryId() {
      let { category3Id } = this.cgForm;
      this.$emit("getCategoryId", { categoryId: category3Id, level: "3" });
    },
  },
};
</script>

<style>
</style>